<template>
  <el-dialog @close="close" v-model="dialogVisible" :title="title" width="60%" align-center>
    <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="70px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="ruleForm.title" placeholder="请输入标题" />
      </el-form-item>
      <el-form-item label="媒体地址" prop="mediaurl">
        <el-input v-model="ruleForm.mediaurl" placeholder="请输入媒体地址" />
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <wang-edior v-model="ruleForm.content" height="50vh" v-if="dialogVisible" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" :loading="saveLoading" @click="handleConfim">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
import { ElMessageBox, ElMessage, FormInstance, ElNotification } from 'element-plus'
import { computed, reactive, ref } from 'vue'
import { helperAdd, helperEdit, getHelperEdit } from '@/api/admin'
import WangEdior from '@/components/WangEdior/index.vue'

const ruleFormRef = ref()
const dialogVisible = ref(false)
const title = ref('新增')
const rules = reactive({
  title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
  content: [{ required: true, message: '请输入内容', trigger: 'blur' }],
})
const ruleForm = reactive({
  id: "",
  title: "",
  content: "",
  mediaurl: "",
})
const saveLoading = ref(false)
const emit = defineEmits(['refresh'])

function close() {
  ruleFormRef.value.resetFields()
  Object.keys(ruleForm).forEach((key) => {
    ruleForm[key] = ""
  })
}

const show = (item = {}) => {
  title.value = '新增'
  if (item.id) {
    title.value = '编辑'
    getHelperEdit({ id: item.id }).then(res => {
      if (res.code == 200) {
        if (res.data) {
          Object.keys(ruleForm).forEach((key) => {
            ruleForm[key] = res.data[key]
          })
          dialogVisible.value = true
          console.log(ruleForm['content']);
        }
      }
    })
  }else{
    dialogVisible.value = true
  }
}

const handleConfim = async () => {
  await ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      saveLoading.value = true
      if (ruleForm.id) {
        //编辑
        helperEdit(ruleForm).then((res) => {
          if (res.code == 200) {
            ElNotification({
              title: '编辑成功',
              message: res.msg,
              type: 'success',
            })
            emit('refresh')
          }
          dialogVisible.value = false
          saveLoading.value = false
        })
      } else {
        //新增
        helperAdd(ruleForm).then((res) => {
          if (res.code == 200) {
            ElNotification({
              title: '添加成功',
              message: res.msg,
              type: 'success',
            })
            emit('refresh')
          }
          dialogVisible.value = false
          saveLoading.value = false
        })
      }
    }
  })
}
defineExpose({
  show,
})
</script>
<style lang="scss" scoped></style>

